/*
 * Copyright (c) 2012 Johan Fylling
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 */

part of engine;


abstract class Action {
}


class MovementAction implements Action {
  final Vector2 _direction   = new Vector2(0,0);
  final Vector2 _orientation = new Vector2(0,0);
  num           speed        = 0.0;
  
  MovementAction ();
  
  Vector2 get direction => new Vector2.copy(this._direction);
  
  void set direction (Vector2 other) { this._direction.clone(other.normalized()); }
  
  Vector2 get orientation => new Vector2.copy(this._orientation);
  
  void set orientation (Vector2 other) { this._orientation.clone(other.normalized()); }
  
  void clone (MovementAction other) {
    this._direction.clone(other._direction);
    this._orientation.clone(other._orientation);
    this.speed = other.speed;
  }
}


class UseAction implements Action {
  int tool;
  
  UseAction (int this.tool);
}


class Actor extends WorldObject {
  LogicController      controller;
  Percept              percept;
  final FieldOfView    fieldOfView    = new FieldOfView(20);
  final MovementAction movementAction = new MovementAction();
  final List<Action>   actions        = new List<Action>();
  num                  maxSpeed;
  
  Actor (LogicController this.controller, Percept this.percept, [num this.maxSpeed = 0]);
  
  String toString () => "a[$controller, $id]";
  
  toJson () {
    var data = super.toJson();
    assert(data is Map);
    data["type"] = "actor";
    data["controller"] = controller;
    data["percept"] = percept;
    data["max_speed"] = maxSpeed;
    return data;
  }
}


class ActorStorageHandler extends WorldObjectStorageHandler {
  call (StorageManager manager, Map attributes) {
    var controllerAttributes = attributes["controller"];
    assert(controllerAttributes is Map);
    var controller = manager.load(controllerAttributes);
    assert(controller is LogicController);
    
    var perceptAttributes = attributes["percept"];
    assert(perceptAttributes is Map);
    var percept = manager.load(perceptAttributes);
    assert(percept is Percept);
    
    Actor actor = new Actor(controller, percept);
    
    var maxSpeed = attributes["max_speed"];
    if (maxSpeed != null) {
      actor.maxSpeed = maxSpeed;
    }
    
    WorldObjectStorageHandler.setAttributes(manager, attributes, actor);
    
    return actor;
  }
}
